'use strict' import { getChannels, createChannel } from '../../../src/amio/amioChannels' import { authenticate, checkRole } from '../../../auth' import { Operation } from 'express-openapi' /** * Operations on /bots/{id}/channels */ export default function() { const GET: Operation = async (req, res, next) => { const { isAllowedUser, role } = req.user if (!isAllowedUser && !checkRole(role, 'edit')) { console.error('No permission to get channels') res.status(401).send() } else { try { res.status(200).json(await getChannels()) } catch (error) { res.status(error.code || 500).send(error.message) } } } const POST: Operation = async (req, res, next) => { const { isAllowedUser, role } = req.user if (!isAllowedUser && !checkRole(role, 'edit')) { console.error('No permission to create a channel') res.status(401).send() } else { try { res.status(200).json(await createChannel(req.body, req.params.id)) } catch (error) { res.status(error.code || 500).send(error.message) } } } return { GET: [authenticate(['allowed-users', 'bot-token']), GET], POST: [authenticate(['allowed-users', 'bot-token']), POST] } }